#include "KinectSensor.h"
#include "QKinectManager.h"

KinectSensor::KinectSensor (const QString& name,
		NUI_IMAGE_TYPE depthType, NUI_IMAGE_RESOLUTION depthRes,
		NUI_IMAGE_TYPE colorType, NUI_IMAGE_RESOLUTION colorRes,
		const QString& intrinsicFile, const QString& extrinsicFile,
		bool bSeatedSkeletonMode, bool bNearMode, bool bFallbackToDefault)
		: Sensor (name, intrinsicFile, extrinsicFile), _pKinect (NULL) {
	_pKinect = QKinectManager::startNewKinect (depthType, depthRes,
			colorType, colorRes, bSeatedSkeletonMode, bNearMode,
			bFallbackToDefault);
	_pKinect->registerNewFrameCallback (this, SLOT(handleQKinectNewFrame(void)));
	__sensorParameter.rgbPixelWidth = 4;
	__sensorParameter.frameWidth = _pKinect->getColorConfig ().frameSize.width ();
	__sensorParameter.frameHeight = _pKinect->getColorConfig ().frameSize.height ();
	__sensorParameter.depthPixelWidth = 2;
	__sensorParameter.frameWidth = _pKinect->getDepthConfig ().frameSize.width ();
	__sensorParameter.frameHeight = _pKinect->getDepthConfig ().frameSize.height ();
	__sensorParameter.frameDepth = 10000;
	__sensorParameter.frameSize =  __sensorParameter.frameWidth *
			__sensorParameter.frameHeight;
}

KinectSensor::KinectSensor (int idx, const QString& name,
		NUI_IMAGE_TYPE depthType, NUI_IMAGE_RESOLUTION depthRes,
		NUI_IMAGE_TYPE colorType, NUI_IMAGE_RESOLUTION colorRes,
		const QString& intrinsicFile, const QString& extrinsicFile,
		bool bSeatedSkeletonMode, bool bNearMode, bool bFallbackToDefault)
		: Sensor (name, intrinsicFile, extrinsicFile), _pKinect (NULL) {
	_pKinect = QKinectManager::startKinectByIndex (idx, depthType, depthRes,
			colorType, colorRes, bSeatedSkeletonMode, bNearMode,
			bFallbackToDefault);
	_pKinect->registerNewFrameCallback (this, SLOT(handleQKinectNewFrame(void)));
	__sensorParameter.rgbPixelWidth = 4;
	__sensorParameter.frameWidth = _pKinect->getColorConfig ().frameSize.width ();
	__sensorParameter.frameHeight = _pKinect->getColorConfig ().frameSize.height ();
	__sensorParameter.depthPixelWidth = 2;
	__sensorParameter.frameWidth = _pKinect->getDepthConfig ().frameSize.width ();
	__sensorParameter.frameHeight = _pKinect->getDepthConfig ().frameSize.height ();
	__sensorParameter.frameDepth = 10000;
	__sensorParameter.frameSize =  __sensorParameter.frameWidth *
			__sensorParameter.frameHeight;
	_index = idx;
}

KinectSensor::~KinectSensor ()
{
	delete _pKinect;
}

void KinectSensor::handleQKinectNewFrame ()
{
	__depthMetaData = _pKinect->getDepthImageInColorCoord ();
	__rgbMetaData = _pKinect->getColorImage ();
	__playerMetaData = _pKinect->getPlayerImageInColorCoord ();
	__handleNewData ();
}

void KinectSensor::start ()
{
	_pKinect->start ();
}

void KinectSensor::stop ()
{
	_pKinect->stop ();
}

QKinect* KinectSensor::getKinect () const
{
	return _pKinect;
}

int KinectSensor::getSensorCount ()
{
	int count;
	HRESULT r = NuiGetSensorCount (&count);
	if (FAILED (r))
		return -1;
	return count;
}
